package com.hsmpay.mobile.service.user.impl;

import com.hsmpay.common.service.impl.BaseServiceSupport;
import com.hsmpay.common.util.DateConvertUtil;
import com.hsmpay.mobile.dao.user.SendmsgLogDao;
import com.hsmpay.mobile.pojo.user.SendmsgLog;
import com.hsmpay.mobile.service.user.SendmsgLogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 用户发送信息日志 服务接口实现类 sendmsgLogService
 *
 * @param <Entity>
 * @param <Query>
 * @author lyc
 *         建立时间：2015-4-28
 */
@Service("sendmsgLogService")
public class SendmsgLogServiceImpl extends BaseServiceSupport<SendmsgLog, Long> implements SendmsgLogService<SendmsgLog, Long> {

    @Resource(name = "sendmsgLogDao")
    private SendmsgLogDao<SendmsgLog, Long> sendmsgLogDao;

    //验证一个终端在90内发送短信是否发过短信
    public boolean isSendOn90s(String uuId, Long terminalTypeId, Long roleId) throws Exception {
        Date now = new Date();

        SendmsgLog sendmsgLog = new SendmsgLog();
        sendmsgLog.setUuId(uuId);
        sendmsgLog.setStatus(1);
        sendmsgLog.setTerminalTypeId(terminalTypeId);
        sendmsgLog.setRoleId(roleId);
        sendmsgLog = sendmsgLogDao.searchFirstEntity(sendmsgLog);

        if (sendmsgLog == null) {//没有发送过
            return false;
        }

        long between = (now.getTime() - sendmsgLog.getCreateDate().getTime()) / 1000;//除以1000是为了转换成秒
        if (between < 90) {
            return true;
        }

        return false;
    }

    //验证一个终端在90内发送短信是否发过短信
    public boolean isSendOn60s(String uuId, Long terminalTypeId, Long roleId) throws Exception {
        Date now = new Date();

        SendmsgLog sendmsgLog = new SendmsgLog();
        sendmsgLog.setUuId(uuId);
        sendmsgLog.setStatus(1);
        sendmsgLog.setTerminalTypeId(terminalTypeId);
        sendmsgLog.setRoleId(roleId);
        sendmsgLog = sendmsgLogDao.searchFirstEntity(sendmsgLog);

        if (sendmsgLog == null) {//没有发送过
            return false;
        }

        long between = (now.getTime() - sendmsgLog.getCreateDate().getTime()) / 1000;//除以1000是为了转换成秒
        if (between < 60) {
            return true;
        }
        return false;
    }

    //验证一个终端一天内是否发送超过30条验证信息
    public boolean isExceed30(String uuId, Long terminalTypeId, Long roleId) throws Exception {
        Date date = new Date();
        String day = DateConvertUtil.dateString(date);
        String yesterday = DateConvertUtil.dateString(DateConvertUtil.getSpecifiedDayBefore(date));

        SendmsgLog sendmsgLog = new SendmsgLog();
        sendmsgLog.setUuId(uuId);
        sendmsgLog.setStatus(1);//发送成功
        sendmsgLog.setTerminalTypeId(terminalTypeId);
        sendmsgLog.setRoleId(roleId);
        sendmsgLog.setStartDate(DateConvertUtil.stringDateTime(yesterday + " 23:00:00"));
        sendmsgLog.setEndDate(DateConvertUtil.stringDateTime(day + " 23:00:00"));

        int count = sendmsgLogDao.getEntityCount(sendmsgLog);

        if (count >= 30) {
            return true;
        } else {
            return false;
        }
    }


}
